package com.orange.cloud.controller;

import com.orange.cloud.domain.CommonResult;
import com.orange.cloud.domain.User;
import com.orange.cloud.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    private Logger LOGGER =  LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserService userService;

    @PostMapping("/create")
    public CommonResult create(@RequestBody User user){
        this.userService.create(user);
        return CommonResult.ok("操作成功！");
    }

    @GetMapping("/{id}")
    public CommonResult<User> getUser(@PathVariable Long id){
        User user = userService.getUser(id);
        LOGGER.info("根据ID获取用户信息，用户名称为：{}", user.getUsername());
        return new CommonResult<>(user);
    }

    @GetMapping("/getUserByIds")
    public CommonResult<List<User>> getUserByIds(@RequestParam List<Long> ids){
        List<User> userList = this.userService.getUserByIds(ids);
        LOGGER.info("根据ID获取用户信息，用户列表为：{}", userList);
        return new CommonResult<>(userList);
    }

    @GetMapping("/getByUsername")
    public CommonResult<User> getByUsername(@RequestParam String username){
        User byUsername = this.userService.getByUsername(username);
        return new CommonResult<>(byUsername);
    }

    @PostMapping("/update")
    public CommonResult update(@RequestBody User user){
        this.userService.update(user);
        return CommonResult.ok("操作成功");
    }

    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Long id){
        this.userService.delete(id);
        return CommonResult.ok("操作成功！");
    }
}
